package org.marsdonne.audits;

import com.fasterxml.jackson.databind.JsonNode;
import com.vladmihalcea.hibernate.type.json.JsonBinaryType;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.TypeDef;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import java.util.Date;

/**
 * （业务执行的）记录/日志/便条（审计）域
 * 用于追溯业务逻辑在执行过程中的蛛丝马迹
 */
@Entity
@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
@NoArgsConstructor
@TypeDef(typeClass = JsonBinaryType.class, defaultForType = JsonNode.class)
public class Note extends LoggedEntity {
    @Column(nullable = false)
    private String serviceId;//服务标识

    @Column(nullable = false)
    private String processId;//进程标识

    @Column(nullable = false)
    private Noted.Level level;

    @Column(nullable = false, columnDefinition = "jsonb")
    private JsonNode message;

    public Note(String serviceId, String processId, Date dateRecorded, Noted.Level level, JsonNode message) {
        this.serviceId = serviceId;
        this.processId = processId;
        this.dateLogged = dateRecorded;
        this.level = level;
        this.message = message;
    }

    public Note(Noted noted) {
        this(noted.getServiceId(), noted.getProcessId(), noted.getDateRecorded(), noted.getLevel(), noted.getMessage());
    }
}
